package jpademo.service;

import java.util.HashSet;
import java.util.Set;

import jpademo.model.Author;
import jpademo.model.Book;
import jpademo.repository.AuthorRepository;
import jpademo.repository.BookRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookService {

	
    @Autowired
    private BookRepository bookRepository;
    @Autowired
    private AuthorRepository authorRepository;
    
    public void many2manyDemo()
    {
        Author author1 = new Author();
        author1.setAuthorID(1);
        author1.setName("Peter");
        
        Author author2 = new Author();
        author2.setAuthorID(2);
        author2.setName("Tom");
        
        Author author3 = new Author();
        author3.setAuthorID(3);
        author3.setName("Ben");
        
        Book javaBook = new Book();
        javaBook.setBookID(1);
        javaBook.setName("Java");

        Book dbBook = new Book();
        dbBook.setBookID(2);
        dbBook.setName("Oracle");
        
        Set<Author> javaAuthors = new HashSet<Author>();
        javaAuthors.add(author1);
        javaAuthors.add(author3);
        
        Set<Author> dbAuthors = new HashSet<Author>();
        dbAuthors.add(author2);
        dbAuthors.add(author3);
        
        javaBook.setAuthors(javaAuthors);
        dbBook.setAuthors(dbAuthors);
        
        bookRepository.save(javaBook);
        bookRepository.save(dbBook);
        //stuRepository.delete(s);
    }
}